package com.nuatar.meibi;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.nuatar.meibi.pubvar.Configer;
import com.nuatar.meibi.pubvar.ImagePair;
import com.nuatar.meibi.pubvar.LAdapter;
import com.nuatar.meibi.pubvar.NetBus;
import com.nuatar.meibi.pubvar.SqlBus;
import com.nuatar.meibi.pubvar.NetBus.ProgressMan;
import com.nuatar.meibi.pubvar.TagsGridView;
import com.nuatar.meibi.pubvar.WallPaper;
import com.nuatar.meibi.pubvar.LAdapter.ViewBinder;
import com.nuatar.meibi.pubvar.ntImageView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

public class ImagePool extends FatherActivity {
	WallPaper wp;
	TagsGridView gvs;
	List<ImagePair> mlist;
	LAdapter la;
	static Handler mhandler;
	MeiBi mb;
	Date dts;
	private int colnum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_pool);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		colnum = dm.widthPixels / Configer.ImageWidth;
		mlist=new ArrayList<ImagePair>();
		CrashHandler crashHandler = CrashHandler.getInstance();  
        crashHandler.init(this); 
		mb=(MeiBi) getApplication();
		wp=mb.CurrentPaper;
		TextView vts=(TextView) findViewById(R.id.ViewText);
		wp.viewc=wp.viewc==null?"未知":wp.viewc;
		vts.setText(wp.title+"	下载:"+wp.viewc);
		gvs =(TagsGridView) findViewById(R.id.gvnews);
		gvs.setHorizontalSpacing(5);
		gvs.setVerticalSpacing(5);
		gvs.setNumColumns(colnum);
		gvs.setPadding(0, 0, 0, 0);
		mlist=Arrays.asList(wp.ShortImages);
		ViewBinder vb = new ViewBinder() {
			@Override
			public View getView(int position, Context ctx, Object data) {
				GridView.LayoutParams param = new GridView.LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
				ntImageView niv=new ntImageView(ctx,(ImagePair)data,wp.id,position);
				niv.setLayoutParams(param);
				niv.setPadding(0, 0, 0, 0);
				return niv;
			}
			@Override
			public View update(View v,int position,Object data) {
				// TODO 自动生成的方法存根
				((ntImageView)v).setImageId(wp.id);
				return v;
				
			}
		};
		
		la=new LAdapter(mb, mlist, vb);
		gvs.setClickable(true);
		gvs.setFocusable(true);
		gvs.setGravity(Gravity.CENTER);
		gvs.setVerticalSpacing(5);
		gvs.setHorizontalSpacing(5);
		gvs.setAdapter(la);
		gvs.setSelector(new ColorDrawable(Color.TRANSPARENT));
		gvs.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent();
				intent.setClass(ImagePool.this, ImageShow.class);
				mb.Curr = ((ntImageView) view).position;
				startActivity(intent);
				
			}});
		mhandler=new Handler(){
			
			@Override
			public void handleMessage(Message msg) {
				la.notifyDataSetChanged();
				
			}
		};
		
		Mt mt=new Mt();
		new Thread(mt).start();
		wpThread wpt = new wpThread(mb);
		new Thread(wpt).start();
	}

	class Mt implements Runnable{
		ProgressDialog pdialog=ProgressDialog.show(ImagePool.this, null, "加载中，请稍后……");
		@Override
		public void run() {
			try {
				Thread.sleep(0);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			// TODO 自动生成的方法存根
			for(int i=0;i<wp.ShortImages.length;i++){
				if(i==wp.ShortImages.length-1){
				}
				ImagePair ips=wp.ShortImages[i];
				ProgressMan pm=new ProgressMan(){

					@Override
					public void Report(int son, int mom) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void getError(Exception e) {
						// TODO Auto-generated method stub
						
					}};
				boolean bit=NetBus.DownLoadLdtPart(wp.id, ips,pm);
				if(bit){
					la.mlist.set(i,ips);
					ImagePool.mhandler.sendEmptyMessage(i);
				}
				
			}
			pdialog.dismiss();
		}
		
	}
	@Override
	protected void onStart() {
		super.onStart();
		mGaTracker.sendView(this.toString());
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			return false;
		}
		return false;
	}
	class wpThread implements Runnable {
		MeiBi mks;

		public wpThread(MeiBi mk) {
			mks = mk;
		}

		@Override
		public void run() {
			SqlBus sb = new SqlBus(mks.context);
			WallPaper wps = sb.getWallPaper(wp.id);
			if (null == wps) {
				NetBus.AddVc(wp.id);
				sb.StoreWallPaper(wp);
			}
		}

	}
}
